import { ImageProps } from './store'
export function generateFitUrl (data: ImageProps, width: number, height: number, format = ['m_pad']) {
  if (data && data.url) {
    const formatStr = format.reduce((prev, current) => {
      return current + ',' + prev
    }, '')
    data.url = data.url + `?x-oss-process=image/resize,${formatStr}h_${height},w_${width}`
  }
}

interface CheckCondition {
  format?: string[];
  size?: number;
}
type ErrorType = 'format' | 'size' | null
export function beforeUploadCheck (file: File, condition: CheckCondition) {
  const { format, size } = condition
  const isValidFormat = format ? format.includes(file.type) : true
  const isValidSize = size ? (file.size / 1024 / 1024 < size) : true
  let err: ErrorType = null
  if (!isValidFormat) {
    err = 'format'
  }
  if (!isValidSize) {
    err = 'size'
  }
  return {
    passed: isValidFormat && isValidSize,
    err
  }
}

interface testData{
  _id: string;
  name: string;
}

export const ArrToObj = <T extends { _id?: string }>(arr: Array<T>) => {
  return arr.reduce((prev, current) => {
    if (current._id) {
      prev[current._id] = current
    }
    return prev
  }, {} as { [key: string]: T })
}

export const ObjToArr = <T>(obj: {[key: string]: T}) => {
  return Object.keys(obj).map((key) => {
    return obj[key]
  })
}
const testData1: testData[] = [{ _id: 'jsdh', name: 'jj' }]
const result = ArrToObj(testData1)
console.log(result)
const testData2: {[key: number]: testData} = {
  1: { _id: '23', name: 'ksdf' }
}
const result1 = ObjToArr(testData2)
console.log(result1)
